package com.heima.media.task;

import com.alibaba.fastjson.JSON;
import com.heima.media.service.WmNewsService;
import com.heima.model.wemedia.pojos.pojos.WmNews;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mgw
 * @create 2021/10/6 21:11
 */
@Component
public class SyncArticleTask {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @XxlJob("synArticle")
    public ReturnT<String>  synArticle(String param){
        List<WmNews> wmNews = wmNewsService.publishArticles();
        if (wmNews!=null&&wmNews.size()>0){
            Map<Long,Integer> resultData=new HashMap<>();
            for (WmNews wmNew : wmNews) {
                resultData.put(wmNew.getId().longValue(),0);
                wmNew.setStatus(9);
            }
            wmNewsService.updateStatus(wmNews);
            kafkaTemplate.send("syncarticle", JSON.toJSONString(resultData));
        }
        return ReturnT.SUCCESS;
    }

}
